package etxt2db.uima.cpe;


import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.collection.CollectionReader_ImplBase;
import org.apache.uima.examples.SourceDocumentInformation;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.DocumentAnnotation;
import org.apache.uima.resource.ResourceConfigurationException;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;

import etxt2db.api.Document;
import etxt2db.corpus.DocumentLoader;

/**
 * A simple collection reader that reads documents from a list of files or directories in the filesystem. It can be
 * configured with the following parameters:
 * <ul>
 * <li><code>Inputs</code> - list of files or directories.</li>
 * <li><code>Encoding</code> (optional) - character encoding of the input files</li>
 * <li><code>Language</code> (optional) - language of the input documents</li>
 * </ul>
 * 
 * 
 */
public class FileSystemCollectionReader extends CollectionReader_ImplBase {
  /**
   * Name of configuration parameter that must be set to the list of paths to the files or
   * directories specified.
   */
  public static final String PARAM_INPUTS = "Inputs";

  /**
   * Name of configuration parameter that contains the character encoding used by the input files.
   * If not specified, the default system encoding will be used.
   */
  public static final String PARAM_ENCODING = "Encoding";

  /**
   * Name of optional configuration parameter that contains the language of the documents in the
   * input files. If specified this information will be added to the CAS.
   */
  public static final String PARAM_LANGUAGE = "Language";

  /**
   * Name of optional configuration parameter that indicates including
   * the subdirectories (recursively) of the second level of directories (if any).
   */
  public static final String PARAM_SUBDIR = "BrowseSubdirectories";
  
  private List<File> mFiles;

  @SuppressWarnings("unused")
  private String mEncoding;

  private String mLanguage;
  
  private Boolean mRecursive;

  private int mCurrentIndex;

  private DocumentLoader documentLoader;
  
  /**
   * @see org.apache.uima.collection.CollectionReader_ImplBase#initialize()
   */
  public void initialize() throws ResourceInitializationException {
	String[] inputs = (String[]) getConfigParameterValue(PARAM_INPUTS);
	File[] files = new File[inputs.length];
	for(int i = 0; i < inputs.length; i++) {
		files[i] = new File(inputs[i].trim());
	}
    mEncoding  = (String) getConfigParameterValue(PARAM_ENCODING);
    mLanguage  = (String) getConfigParameterValue(PARAM_LANGUAGE);
    mRecursive = (Boolean) getConfigParameterValue(PARAM_SUBDIR);
    if (null == mRecursive) { // could be null if not set, it is optional
      mRecursive = Boolean.FALSE;
    }
    mCurrentIndex = 0;

    documentLoader = new DocumentLoader(null, null);
    
    // if any of the files don't exist, throw exception
    
    for(int i = 0; i < files.length; i++) {
    	if(!files[i].exists()) {
    		throw new ResourceInitializationException(ResourceConfigurationException.DIRECTORY_NOT_FOUND,
    				new Object[] { PARAM_INPUTS, this.getMetaData().getName(), files[i].getPath() });
    	}
    }

    // get list of all files referenced by the list of files, if some are
    // directories and the parameter PARAM_SUBDIR is set to True, it gathers
    // all files recursively
    mFiles = new ArrayList<File>();
    addAllFiles(files,0);
  }
  
  /**
   * This method adds to mFiles all the files possible to gather from the
   * list of files specified by the <code> PARAM_INPUTS </code>.
   * It will add all files within the first level of directories (if any).
   * If mRecursive is true, it will include all files from deeper directories,
   * recursively.
   * 
   * @param filesList	the list of files or directories from 
   * which to add new files to <code>mFiles</code>
   * @param level		the directory level
   */
  private void addAllFiles(File[] filesList, int level) {
	  
	  for(int i = 0; i < filesList.length; i++) {
		  if(filesList[i].isDirectory()) {
			  if(level == 0) {
				  addAllFiles(filesList[i].listFiles(),level++);
			  }
			  else if(mRecursive) {
				  addAllFiles(filesList[i].listFiles(),level++);
			  }
		  } else {
			  mFiles.add(filesList[i]);
		  }
	  }
  }

  /**
   * @see org.apache.uima.collection.CollectionReader#hasNext()
   */
  public boolean hasNext() {
    return mCurrentIndex < mFiles.size();
  }

  /**
   * @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS)
   */
public void getNext(CAS aCAS) throws IOException, CollectionException {
    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new CollectionException(e);
    }

    // open input stream to file
    File file = (File) mFiles.get(mCurrentIndex++);

    // the file might have tags
    // document loader used to be sure 
    // that only the plain text is captured
    List<Document> docs = null;
	try {
		docs = documentLoader.load(file);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    Iterator<Document> docsIter = docs.iterator();
    String text = null;
    if(docsIter.hasNext()) {
    	text = docsIter.next().getContent();
    }
      // put document in CAS
    jcas.setDocumentText(text);

    // set language if it was explicitly specified as a configuration parameter
    if (mLanguage != null) {
      ((DocumentAnnotation) jcas.getDocumentAnnotationFs()).setLanguage(mLanguage);
    }

    // Also store location of source document in CAS. This information is critical
    // if CAS Consumers will need to know where the original document contents are located.
    // For example, the Semantic Search CAS Indexer writes this information into the
    // search index that it creates, which allows applications that use the search index to
    // locate the documents that satisfy their semantic queries.
    SourceDocumentInformation srcDocInfo = new SourceDocumentInformation(jcas);
    srcDocInfo.setUri(file.getAbsoluteFile().toURI().toURL().toString());
    srcDocInfo.setOffsetInSource(0);
    srcDocInfo.setDocumentSize((int) file.length());
    srcDocInfo.setLastSegment(mCurrentIndex == mFiles.size());
    srcDocInfo.addToIndexes();
  }

  /**
   * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#close()
   */
  public void close() throws IOException {
  }

  /**
   * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#getProgress()
   */
  public Progress[] getProgress() {
    return new Progress[] { new ProgressImpl(mCurrentIndex, mFiles.size(), Progress.ENTITIES) };
  }

  /**
   * Gets the total number of documents that will be returned by this collection reader. This is not
   * part of the general collection reader interface.
   * 
   * @return the number of documents in the collection
   */
  public int getNumberOfDocuments() {
    return mFiles.size();
  }

}
